// Copyright 2017 guangbo. All rights reserved.

//常用接口
package lib_util

import (
	"math/rand"
	"os"
	"time"
)

func PathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

func CreateDir(path string) {
	os.MkdirAll(path, os.ModePerm)
}

//GetRandomString 生成指定长度的随机字符串
const randomStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

var r *rand.Rand

func GetRandomString(n int) string {
	bytes := []byte(randomStr)
	result := []byte{}
	if r == nil {
		r = rand.New(rand.NewSource(time.Now().UnixNano()))
	}
	for i := 0; i < n; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}
